package cn.com.wms.Base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.internal.util.StringHelper;
import org.springframework.stereotype.Service;

import cn.com.wms.Base.FilterInfo;
import cn.com.wms.Base.QueryResult;
import cn.com.wms.Base.pageInfo.PagingBean;
import cn.com.wms.dao.MixedDao;
import cn.com.wms.model.BaseModel;



/**
 * 
 * @author zbzhu
 * 创建日期 2011-7-7
 * 类名      DefaultBaseService.java
 * 说明   系统默认的service实现大部分的增删改查功能
 */
public class DefaultBaseService<T> {
	//注入此DAO
	private MixedDao<T> mixedDAO;
	
	public MixedDao<T> getMixedDAO() {
		return mixedDAO;
	}

	public void setMixedDAO(MixedDao<T> mixedDAO) {
		this.mixedDAO = mixedDAO;
	}

//	public <U> int deleteObject(U u){
//		int i = this.mixedDAO.delete_Hibernate_Object(U);
//		return i;
//	}
	

	
	public void updateDeleteFlag(Class cla,String key) {
		BaseModel baseModel=(BaseModel) getObjectById(cla, key);
		baseModel.setDelFlag("1");
		saveObject(baseModel);
	}
	
	
	public boolean findBeanUsed(String sql) {
		List list= getMixedDAO().queryListOfJdbcTemplate(sql);
		if(list==null || list.size()==0) {
			return false;
		}
		return true;	
	}
	
	public int updateOrDeleteObject(final String composedSQL,final Object[] params) {
		int i=this.mixedDAO.delete_Or_Update_Hibernate_ComposedQuery(composedSQL, params);
		return i;
	}
	
	
	public int deleteObjectById(Class<? extends T> cls, Serializable id){
		int i = this.mixedDAO.delete_Hibernate_Object_ById(cls, id);
		return i;
	}
	
	public <U> int updateObject(U u){
		int i = this.mixedDAO.update_Hibernate_Object(u);
		return i;
	}
	
	private BaseModel initBaseModel(BaseModel baseModel) {
		
		if(StringHelper.isEmpty(baseModel.getId())) {
			baseModel.setCreatedId("");
			baseModel.setCreatedTime(new Date());
		}else {
			baseModel.setModifyId("");
			baseModel.setModifyTime(new Date());
		}
		if(StringHelper.isEmpty(baseModel.getDelFlag())) {
			baseModel.setDelFlag("0");
		}
		return baseModel;
	}
	
	
	public <U> int saveObject(U u){
		BaseModel baseModel=(BaseModel)u;
		baseModel=initBaseModel(baseModel);
		int i = this.mixedDAO.save_Hibernate_Object(u);
		return i;
	}
	
	
	public <U>Object saveObjReturn(U u) {

		BaseModel baseModel=(BaseModel)u;
		baseModel=initBaseModel(baseModel);
		return this.mixedDAO.save_Hibernate_ObjResult(baseModel);
	}
	
	public T getObjectById(Class<? extends T> cls, Serializable id){
		return this.mixedDAO.find_ByID_Hibernate_Get(cls, id);
	}
	
	public T loadObjectById(Class<? extends T> cls, Serializable id){
		return this.mixedDAO.findByID_Hibernate_Load(cls, id);
	}
	
	/*可以支持多表*/
	public List queryObjectList(String composedHQL,FilterInfo filters){
		List list = this.mixedDAO.findListObj_Hibernate_D_ComposedHQL(composedHQL, filters);
		return list;
	}
	
	/*需要测试是否支持多表*/
	public List<T> queryBeanList(String composedHQL,FilterInfo filters){
		List<T> list = this.mixedDAO.findList_Hibernate_D_ComposedHQL(composedHQL, filters);
		return list;
	}
	
	public QueryResult queryPageList(String composedListHQL,String composedCountHQL,FilterInfo filters ,PagingBean pageBean){
		QueryResult queryResult = this.mixedDAO.queryPage_Hibernate_D_ComposedHQL(composedListHQL, composedCountHQL, filters, pageBean);
		return queryResult;
	}
	
	public int queryBeanCount(String composedHQL,FilterInfo filters) {
		return this.mixedDAO.findCount_Hibernate_D_ComposedHQL(composedHQL, filters);
	}
}
